//
//  NotificationTableViewCell.m
//  CommentPage
//
//  Created by 欧杨 on 2020/12/3.
//

#import "NotificationTableViewCell.h"
#import "NotificationCellViewModel.h"
#import <Masonry.h>

static void *isRead = &isRead;

@interface NotificationTableViewCell()

@property(strong, nonatomic) NotificationCellViewModel *viewModel;
@property(strong, nonatomic) UIImageView *avatar;
@property(strong, nonatomic) UILabel *userNmae;
@property(strong, nonatomic) UILabel *type;
@property(strong, nonatomic) UILabel *readTag;
@property(strong, nonatomic) UILabel *content;
@property(strong, nonatomic) UILabel *date;

@end

@implementation NotificationTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews
{
    self.contentView.layer.borderColor = UIColor.lightGrayColor.CGColor;
    self.contentView.layer.borderWidth = 1;
    
    _avatar = [[UIImageView alloc] init];
    _avatar.layer.cornerRadius = 40;
    _avatar.layer.masksToBounds = YES;
    _avatar.clipsToBounds = YES;
    [self.contentView addSubview:_avatar];
    
    _userNmae = [[UILabel alloc] init];
    [_userNmae setFont:[UIFont systemFontOfSize:22]];
    [self.contentView addSubview:_userNmae];
    
    _type = [[UILabel alloc] init];
    [_type setFont:[UIFont systemFontOfSize:16]];
    [self.contentView addSubview:_type];
    
    _readTag = [[UILabel alloc] init];
    [_readTag setText:@"NEW"];
    [_readTag setTextAlignment:NSTextAlignmentCenter];
    [_readTag setBackgroundColor:UIColor.redColor];
    [_readTag setTextColor:UIColor.whiteColor];
    _readTag.layer.cornerRadius = 10;
    _readTag.layer.masksToBounds = YES;
    _readTag.clipsToBounds = YES;
    [self.contentView addSubview:_readTag];
    
    _content = [[UILabel alloc] init];
    [_content setFont:[UIFont systemFontOfSize:20]];
    [_content setNumberOfLines:1];
    [_content setLineBreakMode:NSLineBreakByTruncatingMiddle];
    [self.contentView addSubview:_content];
    
    _date = [[UILabel alloc] init];
    [self.contentView addSubview:_date];
    
}

- (void)layoutSubviews
{
    [_avatar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.sizeOffset(CGSizeMake(80, 80));
        make.top.left.equalTo(self.contentView).offset(10);
        make.bottom.equalTo(self.contentView).offset(-10);
    }];
    [_userNmae mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_avatar);
        make.left.equalTo(_avatar.mas_right).offset(10);
    }];
    [_type mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_userNmae);
        make.left.equalTo(_userNmae.mas_right).offset(5);
    }];
    [_date mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_type.mas_bottom);
        make.right.equalTo(self.contentView).offset(-5);
    }];
    [_readTag mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.sizeOffset(CGSizeMake(50, 25));
        make.right.equalTo(self.contentView).offset(-10);
        make.top.equalTo(_date.mas_bottom).offset(10);
    }];
    [_content mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_userNmae.mas_bottom).offset(20);
        make.left.equalTo(_avatar.mas_right).offset(10);
    }];
}

- (void)bindViewModel:(NotificationCellViewModel *)viewModel
{
    _viewModel = viewModel;
    
    [_avatar setImage:_viewModel.avater];
    
    [_userNmae setText:_viewModel.userName];
    
    if ([_viewModel.type isEqual: @"like"]) {
        [_type setText:@"点赞了你的内容"];
    } else if ([_viewModel.type isEqual: @"reply"]) {
        [_type setText:@"评论了你的内容"];
        [_content setText:_viewModel.content];
    }
    
    if (_viewModel.isRead) {
        
        [_readTag setHidden:YES];
    }
    
    [_date setText:[self getTimeString]];
    
    [viewModel addObserver:self forKeyPath:@"isRead" options:NSKeyValueObservingOptionNew context:isRead];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}

#pragma mark -Helper Function
- (NSString *)getTimeString
{
    // 获取当前时时间戳 1466386762.345715 十位整数 6位小数
    NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970];
    // 创建歌曲时间戳(后台返回的时间 一般是13位数字)
    NSTimeInterval createTime = [self.viewModel.date timeIntervalSince1970];
    // 时间差
    NSTimeInterval time = currentTime - createTime;
    
    // 秒转小时
    NSInteger hours = time/3600;
    if (hours<24) {
        return [NSString stringWithFormat:@"%ld小时前",hours];
    }
    //秒转天数
    NSInteger days = time/3600/24;
    if (days < 30) {
        return [NSString stringWithFormat:@"%ld天前",days];
    }
    //秒转月
    NSInteger months = time/3600/24/30;
    if (months < 12) {
        return [NSString stringWithFormat:@"%ld月前",months];
    }
    //秒转年
    NSInteger years = time/3600/24/30/12;
    return [NSString stringWithFormat:@"%ld年前",years];
}

#pragma mark -KVO

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    if (context == isRead) {
        if ([[change valueForKey:NSKeyValueChangeNewKey] boolValue]) {
            [self.readTag setHidden:YES];
        }
    }
}

@end
